/**
 * 
 */
requirejs.config({
    baseUrl:'http://static.auoffice.com/static/js' ,
    waitSeconds: 0,
    paths: {
    	jquery:'libs/jquery.min',
    	validate:'libs/validate/jquery.validate.min',
        datatables: 'libs/dataTables/jquery.dataTables.min',
        btdatatables: 'libs/dataTables/dataTables.bootstrap.min',
        //plugins
        base: 'common/base',
    },
    shim: {
    	'validate': {
    		deps: ['jquery'],
    		exports: 'validate'
    	},
        'datatables': {
            deps: ['jquery'],
            exports: 'datatables'
        },
        'btdatatables': {
            deps: ['jquery'],
            exports: 'btdatatables'
        }, 
    }
});

require([
    'jquery',
 	'common/base',
 	'validate',
 	'datatables',
 	'btdatatables',
],function($,base) {
    var table = $("#data_table").DataTable({
        "searching": true,
        "processing": true,
        "serverSide": true,
        "ordering": false,
        "deferRender": true,
        "ajax": {
            "url": '/article/article/list/',
            "type": "POST"
        },
        "columnDefs": [
            {
                "targets": 0,
                "data": null,
                "name": null,
                "searchable": false,
                "bSortable": false,
                "render": function (data, type, full) {
                    var html = '';
                    html += '<input type="checkbox" style="width:18px;height:18px" name="article_id"  data-id="'+full.id+'"/>';
                    return html;
                }
            },
            {
            	"targets": 1,
            	"data": "topic",
            	"name": "topic",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 2,
            	"data": "user_name",
            	"name": "user_name",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 3,
            	"data": "source",
            	"name": "source",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 4,
            	"data": null,
            	"name": "source_id",
            	"searchable": false,
            	"bSortable": false,
            	"render": function (data, type, full) {
            		var html = '';
            		html += '<a target="_blank" href="'+full.source_url+'">'+full.source_id+'</a>';
            		return html;
            	}
            },
            {
            	"targets": 5,
            	"data": "state_desc",
            	"name": "state_desc",
            	"searchable": false,
            	"bSortable": false
            },
        ],
        "initComplete": function () {
            var api = this.api();
            var column = api.column(5);
            var state_select = $('<select class="form-control" id="_status"><option value="">--状态--</option></select>')
            .appendTo($(column.header()).empty() )
            .on( 'change', function (){
                var val = $.fn.dataTable.util.escapeRegex(
                    $(this).val()
                );
                $("#all").prop("checked",false);
                column.search( val ? '^'+val+'$' : '', true, false ).draw();
            });
            state_select.append(_state());
            
            var column2 = api.column(3);
            var source_select = $('<select class="form-control" id="_source"><option value="">--来源--</option></select>')
            .appendTo($(column2.header()).empty())
            .on( 'change', function (){
            	var val = $.fn.dataTable.util.escapeRegex(
            			$(this).val()
            	);
            	$("#all").prop("checked",false);
            	column2.search( val ? '^'+val+'$' : '', true, false ).draw();
            });
            _source();
        },
        "oLanguage": {
            "sLengthMenu": '每页显示 _MENU_条 &nbsp;',
            "sZeroRecords": "没有找到符合条件的数",
            "sProcessing": "加载中...",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "木有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "«",
                "sNext": "»",
                "sLast": "尾页"
            }
        },
    });
    
    var _state = function() {
        return  '<option value="0">正常</option><option value="9">已删除</option>';
    };  
    //来源下拉框追加option
    var _source = function() {
    	base.services.post('article', 'getArticleSource', {}, function(code, msg, data) {
    		if (code === base.services.CODE_SUCC) {
    			var html = "";
    			for (var comment in data){
    				html += '<option value="'+data[comment].id+'">'+data[comment].name+'</option>';
    			}
    			$("#_source").append(html);
    		}
    	});
    };  
    
    //datatables 行点击事件
    $("#data_table tbody").on('click','tr', function () {
    	var checked = $("td",this).find("input[type=checkbox]");
    	if ($(checked).is(':checked')){
    		$(checked).prop("checked",false);
    	}else{
    		$(checked).prop("checked",true);
    	}
    	var tr_len = $("#data_table tbody tr").find("input[type=checkbox]").length;
    	var check_len = $("#data_table tbody tr").find("input[type=checkbox]:checked").length;
    	if (tr_len == check_len){
    		$("#all").prop("checked",true);
    	}else{
    		$("#all").prop("checked",false);
    	}
    });
    
    //datatables 全选事件
    $("#all").click(function(){
    	if ($(this).is(':checked')){
    		$("#data_table tbody tr").find("input[type=checkbox]").prop("checked",true);
    	}else{
    		$("#data_table tbody tr").find("input[type=checkbox]").prop("checked",false);
    	}
    });
    //查看
    $(".detail").click(function(){
    	var len = $("[name=article_id]:checked").length;
    	if (len != 1){
    		base.layui.alert("信息","请选择一行数据编辑");
    		return;
    	}
    	var article_id = $("[name=article_id]:checked").attr("data-id");
    	var o = "/article/article/detail/?article_id="+article_id;
    	var l = "查看文章详情";
    	base.contabs.addtab(o,l,window.document.location.pathname);
    });
});